﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Domains.zjy;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.PrenatalCheckup
{
    /// <summary>
    /// 获取种母妊检 进行赋值
    /// </summary>
    public class DatailPrenatalCheckupHandler : IRequestHandler<DetailPrenatalCheckupCommand, Result>
    {
        /// <summary>
        /// 种母妊检仓储
        /// </summary>
        private readonly IBaseRepository<PrenatalCheckups> _prenatalCheckupsRepository;

        /// <summary>
        /// 猪只档案仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> _basicInformationRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<DatailPrenatalCheckupHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="prenatalCheckupsRepository"></param>
        /// <param name="basicInformationRepository"></param>
        /// <param name="logger"></param>
        public DatailPrenatalCheckupHandler(IBaseRepository<PrenatalCheckups> prenatalCheckupsRepository,
                                            IBaseRepository<BasicInformation> basicInformationRepository,
                                            ILogger<DatailPrenatalCheckupHandler> logger)
        {
            _prenatalCheckupsRepository = prenatalCheckupsRepository;
            _basicInformationRepository = basicInformationRepository;
            _logger = logger;
        }

        /// <summary>
        /// 获取种母妊检并赋值
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(DetailPrenatalCheckupCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var query = (from a in await _prenatalCheckupsRepository.GetAll()
                             join b in await _basicInformationRepository.GetAll() on a.BasicInforId equals b.BasicInforId
                             where (a.IsPrenatalCheckUp == 1 && a.PrenatalCheckupId == request.PrenatalCheckupId)
                             select new PrenatalCheckupDtos()
                             {
                                 PrenatalCheckupId = a.PrenatalCheckupId,
                                 DocumentNumber = a.DocumentNumber,
                                 SerialNumber = a.SerialNumber,
                                 BasicInforId = a.BasicInforId,
                                 PregnancyTestResultId = a.PregnancyTestResultId,
                                 PregnancyTestTypeId = a.PregnancyTestTypeId,
                                 ParityNumber = a.ParityNumber,
                                 CurrentBuilding = a.CurrentBuilding,
                                 Currentfield = a.Currentfield,
                                 TurnBuilding = a.TurnBuilding,
                                 TurnColumn = a.TurnColumn,
                                 Principal = a.Principal,
                                 PregnancyTestDate = a.PregnancyTestDate,
                                 IsPrenatalCheckUp = a.IsPrenatalCheckUp,
                                 CreatedOwner = a.CreatedOwner,
                                 CreatedTime = a.CreatedTime,
                                 UpdateOwner = a.UpdateOwner,
                                 UpdateTime = a.UpdateTime,
                                 ExamineOwner = a.ExamineOwner,
                                 ExamineTime = a.ExamineTime,
                                 EarNumber = b.EarNumber,//耳号
                             }).FirstOrDefault();
                if (query != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = query;
                    result.Msg = "获取种母妊检并赋值成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "获取种母妊检并赋值异常" + ex.Message;
                _logger.LogError("获取种母妊检并赋值异常" + ex.Message);
            }
            return result;
        }
    }
}
